package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdEliminateUnion;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossStrongholdEliminateUnionDaoImpl extends TempDataObjectDao<CrossStrongholdEliminateUnion> {
    public static String getCrossStrongholdEliminateUnionSql = "select * from t_c_activity_stronghold_eliminate_union where activtyId=? ";

    public Map<Integer, Map<Integer, CrossStrongholdEliminateUnion>> getCrossStrongholdEliminateUnion(int activtyId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, CrossStrongholdEliminateUnion>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossStrongholdEliminateUnionSql);
            int i = 1;
            call.setInt(i++, activtyId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdEliminateUnion union = parse(rs);
                int round = union.getRound();
                Map<Integer, CrossStrongholdEliminateUnion> unionMap = dataMap.get(round);
                if (unionMap == null) {
                    unionMap = new ConcurrentHashMap<>();
                    dataMap.put(round, unionMap);
                }
                unionMap.put(union.getId(), union);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<CrossStrongholdEliminateUnion> getEliminateUnionList(int activtyId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossStrongholdEliminateUnion> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossStrongholdEliminateUnionSql);
            int i = 1;
            call.setInt(i++, activtyId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossStrongholdEliminateUnion union = parse(rs);
                dataList.add(union);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public static String updateCrossStrongholdEliminateUnionSql = "update t_c_activity_stronghold_eliminate_union set unionBaseInfo=?,star=?,hpAdd=?,totalDamage = ?, sortList = ?,serverId = ? where activtyId=? and round=? and id=?  and unionUid=?";

    @Override
    protected boolean updateData(CrossStrongholdEliminateUnion crossStrongholdEliminateUnion) {
        crossStrongholdEliminateUnion.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateUnion.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getStar()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getHpAdd()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateUnion.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateUnion.getSortList())));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateUnion.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getActivtyId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateUnion.getUnionUid()));
        if (getDb().executeNoneQuery(updateCrossStrongholdEliminateUnionSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossStrongholdEliminateUnionSql = "insert into t_c_activity_stronghold_eliminate_union(activtyId,round,id,serverId,unionUid,unionBaseInfo,star,hpAdd,totalDamage,sortList) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossStrongholdEliminateUnion crossStrongholdEliminateUnion) {
        crossStrongholdEliminateUnion.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getActivtyId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossStrongholdEliminateUnion.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossStrongholdEliminateUnion.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossStrongholdEliminateUnion.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getStar()));
        para.put(i++, new DbParameter(Types.INTEGER, crossStrongholdEliminateUnion.getHpAdd()));
        para.put(i++, new DbParameter(Types.NVARCHAR, crossStrongholdEliminateUnion.getTotalDamage().toString()));
        para.put(i++, new DbParameter(Types.NVARCHAR, JSON.toJSONString(crossStrongholdEliminateUnion.getSortList())));
        if (getDb().executeNoneQuery(insertCrossStrongholdEliminateUnionSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossStrongholdEliminateUnion parseData(ResultSet rs) throws SQLException {
        CrossStrongholdEliminateUnion crossStrongholdEliminateUnion = new CrossStrongholdEliminateUnion();
        crossStrongholdEliminateUnion.setActivtyId(rs.getInt("activtyId"));
        crossStrongholdEliminateUnion.setRound(rs.getInt("round"));
        crossStrongholdEliminateUnion.setId(rs.getInt("id"));
        crossStrongholdEliminateUnion.setServerId(rs.getLong("serverId"));
        crossStrongholdEliminateUnion.setUnionUid(rs.getString("unionUid"));
        crossStrongholdEliminateUnion.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        crossStrongholdEliminateUnion.setStar(rs.getInt("star"));
        crossStrongholdEliminateUnion.setHpAdd(rs.getInt("hpAdd"));
        crossStrongholdEliminateUnion.setTotalDamage(new BigInteger(rs.getString("totalDamage")));
        String sortListStr = rs.getString("sortList");
        if (StringUtils.isNullOrEmpty(sortListStr)) {
            crossStrongholdEliminateUnion.setSortList(new ArrayList<>());
        }else {
            List<Integer> idList = JSON.parseArray(sortListStr, Integer.class);
            crossStrongholdEliminateUnion.setSortList(idList);
        }
        return crossStrongholdEliminateUnion;
    }
}
